using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Gadgets
{
    public partial class TreeMenu : DockContent 
    {
        public TreeMenu()
        {
            InitializeComponent();
        }
        
        private void TreeMenu_Load(object sender, EventArgs e)
        {
            
        }
        public void Restart()
        {           
            DataSet menus = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC020001, new object[2] {DeclareLibrary.USER_NAME, "M" });
            treeView1.Nodes.Clear();
            if (menus != null)
            {
                foreach (DataRow row in menus.Tables[0].Rows)
                {
                    if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                    {
                        TreeNode root = new TreeNode();
                        root.Text = row["MENU_TEXT"].ToString();
                        root.Name = row["MENU_ID"].ToString();                        
                        treeView1.Nodes.Add(root);
                        AddTreeNode(ref root, menus);
                    }
                }
            }
            treeView1.ExpandAll();
        }
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Name))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["MENU_TEXT"].ToString();
                    child.Name = row["MENU_ID"].ToString();
                    if (row["FORM_NAME"] != DBNull.Value)
                    {
                        child.Tag = new string[4] { row["FORM_NAME"].ToString(), 
                            row["PARAMETER"].ToString(), row["DOCK"].ToString(), row["FORM_TYPE"].ToString() };                        
                        child.StateImageIndex = 1;
                    }
                    else
                    {
                        child.StateImageIndex = 0;
                    }
                    
                    parent.Nodes.Add(child);
                    AddTreeNode(ref child, menus);
                }                
            }
        }
       
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                if (e.Node.Nodes.Count > 0)
                    return;
                if (e.Node.Tag != null)
                {
                    string[] node_tag = (string[])e.Node.Tag;
                    if (node_tag[3] == "S")
                    {
                        foreach (DockContent childForm in this.DockPanel.Contents)
                        {
                            if (childForm.GetType().ToString() == node_tag[0])
                            {
                                childForm.Text = e.Node.Text;
                                childForm.Tag = node_tag[1];
                                childForm.Activate();
                                return;
                            }
                        }
                    }
                    DockContent obj = (DockContent)Activator.CreateInstance(
                        Type.GetType(node_tag[0], true, true));
                    obj.Text = e.Node.Text;
                    obj.Tag = node_tag[1];
                    switch (node_tag[2])
                    {
                        case "B":
                            obj.Show(this.DockPanel, DockState.DockBottom);
                            break;
                        case "T":
                            obj.Show(this.DockPanel, DockState.DockTop);
                            break;
                        case "L":
                            obj.Show(this.DockPanel, DockState.DockLeft);
                            break;
                        case "R":
                            obj.Show(this.DockPanel, DockState.DockRight);
                            break;
                        case "F":
                            obj.Show(this.DockPanel, DockState.Float);
                            break;
                        default:
                            obj.Show(this.DockPanel, DockState.Document);
                            break;
                    }

                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            
        }
    }
}